package org.j4me.deviceinfo.data;

import java.util.StringTokenizer;

import org.dom4j.Element;

public class Key {
	private int _key;
	private String _name;
	private int _gameaction;

	public Key(String key, String value) {
		
		StringTokenizer words = new StringTokenizer(key,"K[]");
		_key = Integer.parseInt(words.nextToken());
		
		words = new StringTokenizer(value," {},");
		try
		{
		_gameaction = Integer.parseInt(words.nextToken());
		}catch( Exception e ){};
		if(words.hasMoreElements())
			_name = words.nextToken();
	}
	
	public Key(Element elt) {
		
		_key = Integer.parseInt(elt.attributeValue("key"));
		_name = elt.attributeValue("name");
		_gameaction = Integer.parseInt(elt.attributeValue("game"));
		
		
	}
	
	public void toXml(Element elt)
	{
		
		elt.addAttribute("key", "" + _key);
		elt.addAttribute("game", "" + _gameaction);
		elt.addAttribute("name", DeviceDb.encode(_name));
		
	}
	
	
	
	

	public boolean equals(Object obj) {
		if (obj instanceof Key) {
			Key key = (Key) obj;
			return ( _key == key._key && key._gameaction == _gameaction && (_name == key._name || (_name != null && _name.equals(key._name) )));
		}
		return super.equals(obj);
	}

	public int getKey() {
		return _key;
	}

	public String getName() {
		return _name;
	}

	public int getGameaction() {
		return _gameaction;
	}
}
